package org.dromara.house.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.house.domain.bo.ParkHousingBo;
import org.dromara.house.domain.entity.*;
import org.dromara.house.mapper.ParkFloorMapper;
import org.dromara.house.mapper.ParkHousingDetailsMapper;
import org.dromara.house.mapper.ParkHousingMapper;
import org.dromara.house.service.ParkHousingDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ParkHousingDetailsServiceImpl extends ServiceImpl<ParkHousingDetailsMapper, ParkHousingDetails> implements ParkHousingDetailsService {

    @Autowired
    private ParkHousingMapper parkHousingMapper;
    @Autowired
    private ParkFloorMapper parkFloorMapper;

    @Override
    public TableDataInfo<ParkHousingDetails> selectPageList(ParkHousingBo bo, PageQuery pageQuery) {
        Page<ParkHousingDetails> page = baseMapper.selectPageList(pageQuery.build(),bo);
        return TableDataInfo.build(page);
    }

    @Override
    public ParkHousingDetails selectById(Long id) {
        return baseMapper.selectParkHousingDetailsById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertParkHousingDetails(ParkHousingDetails parkHousingDetails) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        parkHousingDetails.setCreateTime(new Date());
        parkHousingDetails.setCreateBy(loginUser.getUserId());
        parkHousingDetails.setDeleteStatus(0);
        return baseMapper.insert(parkHousingDetails);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Void> updateParkHousingDetails(ParkHousingDetails parkHousingDetails) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        parkHousingDetails.setUpdateTime(new Date());
        parkHousingDetails.setUpdateBy(loginUser.getUserId());
        //查询当前房间是否绑定有未到期并且未退租的合同，有的话不让保存
        ParkContract parkContract = baseMapper.selectParkContractByHouseId(parkHousingDetails.getId(),parkHousingDetails.getHousingId());
        if (ObjectUtil.isNotEmpty(parkContract)) {
          return R.fail("当前房间有未到期的合同,不能变更！");
        }
         baseMapper.updateById(parkHousingDetails);
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Long[] ids) {
        for (Long id : ids) {
            ParkHousingDetails parkHousingDetails = baseMapper.selectById(id);
            parkHousingDetails.setDeleteStatus(1);
            baseMapper.updateById(parkHousingDetails);
        }
        return 1;
    }

    @Override
    public List<ParkFloor> getParkFloor(Long housingId) {
        LambdaQueryWrapper<ParkFloor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkFloor::getHousingId, housingId);
        wrapper.orderByAsc(ParkFloor::getId);
       return parkFloorMapper.selectList(wrapper);
    }
}
